package com.sdcqjy.property.presenter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdcqjy.mylibrary.http.BaseRequestBack;
import com.sdcqjy.mylibrary.http.BaseRet;
import com.sdcqjy.mylibrary.http.HttpClient;
import com.sdcqjy.property.base.BaseContract;
import com.sdcqjy.property.mode.LoginMode;
import com.sdcqjy.property.presenter.contract.LoginContract;
import com.sdcqjy.property.presenter.contract.a;

import java.util.HashMap;
import java.util.Map;

import static com.sdcqjy.mylibrary.http.HttpClient.HttpType.JSON;
import static com.sdcqjy.property.presenter.contract.a.Login;

/**
 * Created by 磁磁帅 on 2018/4/14.
 * 功能：
 */
public class LoginPresenter extends BaseContract.Presenter<LoginContract.View> {
    public LoginPresenter(LoginContract.View baseView) {
        super(baseView);
    }

    public void sendWChatCode(String code) {
        new HttpClient()
                .addParam("code", code)
                .setHttpType(HttpClient.HttpType.GET)
                .httpRequest(a.SendWChatCode, new BaseRequestBack<BaseRet<LoginMode>>() {
                    public BaseRet<LoginMode> jsonToMode(String jsonStr) {
                        BaseRet<LoginMode> ret = new Gson().fromJson(jsonStr, new TypeToken<BaseRet<LoginMode>>() {
                        }.getType());
                        return ret;
                    }

                    public void onResponseSucc(BaseRet<LoginMode> ret) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            if (ret.isOk()) {
                                baseView.loginSucc(ret.data);
                            } else {
                                baseView.applyError(ret.msg);
                            }
                        }
                    }

                    public void onResponseFail(String retStr) {
                        if (baseView != null) {
                            baseView.dismissDialog();
                            baseView.applyError(retStr);
                        }
                    }
                });
        if (baseView != null) baseView.openLoadDialog();
    }

    public void login(String userName, String pwd) {
        Map<String, Object> param = new HashMap<>();
        param.put("userName", userName);
        param.put("passWord", pwd);
        param.put("userType", "1");
        param.put("isLogin", "1");
        new HttpClient()
                .setParam(param)
                .setHttpType(JSON).httpRequest(Login, new BaseRequestBack<BaseRet<LoginMode>>() {
            public BaseRet<LoginMode> jsonToMode(String jsonStr) {
                return new Gson().fromJson(jsonStr, new TypeToken<BaseRet<LoginMode>>() {
                }.getType());
            }

            public void onResponseSucc(BaseRet<LoginMode> ret) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    if (ret.isOk()) {
                        baseView.loginSucc(ret.data);
                    } else baseView.applyError(ret.msg);
                }
            }

            public void onResponseFail(String retStr) {
                if (baseView != null) {
                    baseView.dismissDialog();
                    baseView.applyError(retStr);
                }
            }
        });
        if (baseView != null) baseView.openLoadDialog();
    }
}
